﻿using System;
using System.Windows.Forms;
using InfoboardAdapterC.Connection;
using InfoboardAdapterC.Parameter;
using InfoboardAdapterC.RestOperation;
using NKnife.IoC;
using NKnife.NLog.WinForm;

namespace InfoboardAdapterC
{
    public partial class MainRun : Form
    {
        private readonly LoggerListView _LoggerListView = LoggerListView.Instance;
        private readonly MainRunViewModel _InfoViewRest = DI.Get<MainRunViewModel>();
        public MainRun()
        {
            InitializeComponent();
            InitDgv();
            InitControls();
            RunStar();
        }

        private bool InitList()
        {
            try
            {
                var list = _InfoViewRest.GetHidTable();
                HidComboBox.DataSource = list;
                HidComboBox.DisplayMember = list.Columns[1].ColumnName;
                HidComboBox.ValueMember = list.Columns[0].ColumnName;
                HidComboBox.SelectedIndex = 0;
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"初始化系统失败，原因[{ex.Message}]，请重新配置数据库连接");
            }
            return false;
        }

        private void RunStar()
        {
            if (!_InfoViewRest.ConnectionHave)
            {
                ShowSystem();
                return;
            }
            if (InitList())
            {
                if (RunButton.Text == "启动")
                {
                    RunButton.Text = "停止";
                    _InfoViewRest.Start();
                }
                else
                {
                    RunButton.Text = "启动";
                    _InfoViewRest.Stop();
                }
                button1.Enabled = RunButton.Text == "启动";
            }
        }

        private void InitControls()
        {
            NlogPanel.Controls.Add(_LoggerListView);
            _LoggerListView.BringToFront();
            _LoggerListView.Dock = DockStyle.Fill;
            HiddenSystem();
        }

        private void MainRun_Load(object sender, EventArgs e)
        {

        }
        private void ConfigToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new ParameterConfigView().ShowDialog();
        }

        private void ConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new ConnectionView().ShowDialog();
        }

        private void RunButton_Click(object sender, EventArgs e)
        {
            RunStar();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _InfoViewRest.RunRest();
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowSystem();
        }

        private void ShowSystem()
        {
            //窗体显示
            WindowState = FormWindowState.Normal;
            //不显示任务栏
            ShowInTaskbar = true;
            Show();
        }

        /// <summary>
        /// 系统窗体隐藏
        /// </summary>
        private void HiddenSystem()
        {

            //窗体显示
            WindowState = FormWindowState.Minimized;
            //不显示任务栏
            ShowInTaskbar = false;
            Hide();
        }

        private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void notifyIconAdapter_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(WindowState!=FormWindowState.Minimized)
                HiddenSystem();
            else
            {
                ShowSystem();
            }
           
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void HidComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_InfoViewRest.ConnectionHave)
            {
                BindDataSource();
            }
        }

        private void BindDataSource()
        {
            if (HidComboBox.SelectedValue.ToString().Length <= 0) return;
            var dt = _InfoViewRest.GeTable(HidComboBox.SelectedValue.ToString());
            if (dt != null)
                dataGridView1.DataSource = dt;
        }

        private void InitDgv()
        {
            dataGridView1.Columns.Add(CreatNewColumn(nameof(BoardInfoItem.itemId), "项目编码"));
            dataGridView1.Columns.Add(CreatNewColumn(nameof(BoardInfoLibrary.ItemName),"项目名称"));
            dataGridView1.Columns.Add(CreatNewColumn(nameof(BoardInfoItem.buildingAreaId),"科室编码" ));
            dataGridView1.Columns.Add(CreatNewColumn(nameof(BoardInfoItem.itemData),"查询结果"));
            dataGridView1.Columns.Add(CreatNewColumn(nameof(BoardInfoLibrary.ItemInfo),"查询语句"));

          
        }

        private static DataGridViewColumn CreatNewColumn(string name, string headText)
        {
            var result = new DataGridViewTextBoxColumn
            {
                Name = name,
                DataPropertyName = name,
                HeaderText = headText
            };
            return result;
        }
    }
}
